package com.lyz.dataStructure.xiancheng.读写锁;/**
 * @Author:759057893@qq.com Lyz
 * @Date: ${Date} 11:39
 * @Description:
 **/

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 *@Author:759057893@qq.com Lyz
 *@Date: 2019/8/27 11:39
 *@Description:
 **/
public class ReadWriteLockTest {
    private double price1;
    private double price2;
    private ReadWriteLock lock;
    public ReadWriteLockTest() {
        price1 = 1.0;
        price2 = 2.0;
        lock = new ReentrantReadWriteLock();
    }
    public double getPrice1() {
        //读取资源锁定
        lock.readLock().lock();
        double value = price1;
        lock.readLock().unlock();
        return value;
    }
    public double getPrice2() {
        lock.readLock().lock();
        double value = price2;
        lock.readLock().unlock();
        return value;
    }
    public void setPrices(double price1, double price2) {
        lock.writeLock().lock();
        this.price1 = price1;
        this.price2 = price2;
        lock.writeLock().unlock();
    }
}
